#!/usr/bin/env python

import sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from plotDataClass import LPC
from matplotlib.lines import Line2D

import ui_RemoveLineItems

class Remove_Line_Dialog(QDialog, ui_RemoveLineItems.Ui_RemoveItemWidget):
    def __init__(self, activeTraces = None, errorTraces = None, parent = None):
        super(Remove_Line_Dialog, self).__init__(parent)
        self.setupUi(self)
        if parent:
            self.parent = parent
        self.activeplots = activeTraces
        self.errorplots = errorTraces
        self.populate_dialog()
        self.connect(self.listWidget, SIGNAL("itemClicked(QListWidgetItem *)"), self.getPlotItem)
        self.connect(self.rmvLinesBtn,  SIGNAL("clicked()"), self.removeItem)
    

    def removeItem(self):
        items = self.listWidget.selectedItems()
        if items is not None:
            for item in items:
                current_line = self.getPlotItem(item)
                current_line.remove()
                
                errorlist = self.getErrorItem(self.listWidget.currentItem())
                if errorlist:
                    for sublist in errorlist:
                        for err in sublist:
                            err.remove()
                        
                self.parent.rescale_plot()
                self.parent.plotDict.pop(str(item.text()))
                self.parent.lpcDict.pop(str(item.text()))
                self.listWidget.takeItem(self.listWidget.row(item))
            
        
    def getPlotItem(self, item):
        linePlotItem = self.activeplots.get(str(item.text()))
        #self.setupLineOptions(linePlotItem)
        return linePlotItem
    
    def getErrorItem(self, item):
        errorItem = self.errorplots.get(str(item.text()))
        return errorItem        
    
    def populate_dialog(self):
        if self.activeplots is not None and type(self.activeplots) is dict:
            activeList = self.activeplots.keys()
            activeList.sort()
            self.listWidget.addItems(activeList)
        self.listWidget.setCurrentRow(0)
        self.getPlotItem(self.listWidget.currentItem())
    
    
if __name__ == "__main__":
    ydict = {'y':"trash", 'y2':"take", 'y3':"out"}
    dialog = QApplication(sys.argv)
    mydialog = Remove_Line_Dialog(ydict)
    mydialog.show()
    sys.exit(dialog.exec_())
